package cn.crc.web.safe;

import cn.crc.web.tool.TestUserVo;
import cn.crc.web.tool.TestVipVo;
import cn.hutool.core.date.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.*;

/**
 * 线程安全_25.4.16_liuhz
 */
@Component
public class TestSafe1 {
    public static void main(String[] args) {
        //TestSafe1.testClz1();
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void handle(){
        Map map= WarnCacheData.map1;
        System.out.println(DateUtil.now()+";"+map.size());
    }

    private static List<TestUserVo> list1= new CopyOnWriteArrayList<>();
    private static Map<String, TestUserVo> map1= new ConcurrentHashMap<>();
    private static void testClz1(){
        List<TestUserVo> list用户= TestVipVo.getUserTest();
        for(TestUserVo userVo: list用户){
            map1.put(userVo.getTelephone(), userVo);
        }

        ScheduledExecutorService executorService= Executors.newSingleThreadScheduledExecutor();
        executorService.scheduleAtFixedRate(() -> {
            for(TestUserVo userVo: list用户){
                map1.put(userVo.getTelephone(), userVo);
            }
            TestSafe1.testPrint();
        }, 0,5, TimeUnit.SECONDS);//TimeUnit.MINUTES 分钟
    }
    private static void testPrint(){
        Set entrys= map1.entrySet();
        for(Object entry: entrys){
            Map.Entry en= (Map.Entry) entry;
            System.out.println(DateUtil.now()+"\t"+ en.getKey()+";"+en.getValue());
        }
    }

    private static void testClz2(){
    }
}
